//引入代码
var call = require('../../util/request.js');
var qrCode = require('../../util/weapp-qrcode.js');
Page({
  data: {
    
  },
  onShow: function() {
  	
  },
  onReady: function() {
  
  
  },
  onLoad: function(options) {
	//订单详细
	call.getData('api/orderDetail?order_id=' + options.id, this.orderDetail, this.fail);
	
	
	},  
  orderDetail:function(data){
	  console.log('订单详细'+JSON.stringify(data.data))
	  // 二维码生成工具
	  	new qrCode('couponQrcode', {
	  		text: data.data.order_no,
	  		width: 200,
	  		height: 200,
	  		colorDark: "#333333",
	  		colorLight: "#FFFFFF",
	  		correctLevel: qrCode.CorrectLevel.H
	  	})
		
		var weekDay = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
		var myDate = new Date(Date.parse(data.data.goods_time));
		console.log(weekDay[myDate.getDay()]);    // 星期五
		
		
	  this.setData({
	  	orders: data.data,
		weekDay:weekDay[myDate.getDay()]
	  })
	  
  },
  //订单复制
  copy:function(e){
	  console.log('复制'+e.currentTarget.dataset.copy)
	 wx.setClipboardData({
	 data: e.currentTarget.dataset.copy,
	 success: function (res) {
	
	 tt.showToast({
	   icon: 'none',
	   title: '复制成功',
	 });
	 }
	 }) 
  },
  //跳转景区信息
  confirm: function () {
  	  console.log('事件')
    tt.navigateTo({ url: '/pages/verification/verification' })
  }, 
  
  makePhoneCall: function () {
    var that = this
    tt.makePhoneCall({
      phoneNumber: '0574-56155555',
      success: function () {
        // tt.showToast({
        //   title: '请拨打电话',
        // });
      },
      fail (err) {
        tt.showToast({
          icon: 'none',
          title: err.errMsg,
        });
      }
    })
  },
  //申请退款
  refunds:function(e){
	  
	  tt.navigateTo({ url: '/pages/application/application?order_id='+e.currentTarget.dataset.id })
	  // var postData = {
	  // 		  order_id:e.currentTarget.dataset.id
	  // }
	  // call.request('api/orderRefund',postData,this.orderRefund, this.fail);
  },
  
  // orderRefund:function(data){
	 //  console.log('退款返回值'+data)
  // }
		orderDel:function(e){
			console.log('回调'+e.currentTarget.dataset.ids)
			call.delData('api/orderDel?order_id='+e.currentTarget.dataset.ids,this.orderDels, this.fail);
		},
		
		orderDels:function(data){
			
			 tt.switchTab({ url: '/pages/personal/personal' })
		},
		
	//删除订单
	orderDel:function(e){
		  
			console.log('回调'+e.currentTarget.dataset.ids)
		    call.delData('api/orderDel?order_id='+e.currentTarget.dataset.ids,this.orderDels, this.fail);
	},
	
	orderDels:function(data){
		console.log('状态码'+data.code)
		if(data.code==10000){
			 tt.switchTab({ url: '/pages/personal/personal' })
			console.log('删除的回调'+JSON.stringify(data))
			tt.showToast({
				icon: 'none',
				title: '删除成功'
			})
		}else{
			tt.showToast({
				icon: 'none',
				title: data.msg
			})
		}
		
	},	
	
	//申请退款
	refunds:function(e){
		    tt.navigateTo({ url: '/pages/application/application?order_id='+e.currentTarget.dataset.id })
	},
	
	//再次预订
	again:function(e){
		  console.log('id'+e.currentTarget.dataset.id)
		 console.log('名字'+e.currentTarget.dataset.title)
		   tt.navigateTo({
		   	url: '/pages/order/order?id='+e.currentTarget.dataset.id + '&name='+JSON.stringify(e.currentTarget.dataset.title),
		   })
	},
	
	//立即付款
	payment:function(e){
		  console.log('立即付款id'+e.currentTarget.dataset.id)
		  var postData={
			  order_id:e.currentTarget.dataset.id
		  }
		/*  call.request('api/orderPay',postData,this.orderPay, this.fail); */
		
		tt.request({
		  url: "https://dy.nbhysj.com/api/orderPay",
		  data: {
			 order_id:e.currentTarget.dataset.id
		  },
		  method: 'POST',
		  success(data) {
			console.log('进来了吗'+JSON.stringify(data.data.data))
			tt.pay({
			  orderInfo: data.data.data,
			  service: 1,
			  getOrderStatus(res) {
				
			      let { out_order_no } = res;
			      return new Promise(function (resolve, reject) {
				
					tt.request({
					  url: "https://dy.nbhysj.com/api/getOrderStatus",
					  data: {out_order_no:out_order_no},
					  method: 'POST',
					  success(res) {
						 // 商户后端查询的微信支付状态，通知收银台支付结果
						  console.log('经来么'+res.data.data.order_status)
						 if(res.data.data.order_status==2){
						
							 resolve({code:0});
							
						 }
					  },
					  fail(err) {
						reject(err);
					  }
					});  
					
			      });
			    },
			  success(res) {
				  console.log('支付状态查询哈哈'+res.code)
				if(res.code==0){
					var postData = {
							out_order_no:data.data.out_order_no
						}
						tt.request({
						  url: "https://dy.nbhysj.com/api/getOrderStatus",
						  data: postData,
						  method: 'POST',
						  success(res) {
							
							 // 商户后端查询的微信支付状态，通知收银台支付结果
							 if(res.data.data.order_status==2){
								 tt.navigateTo({
								 	url: '/pages/verification/verification?order_no='+res.data.data.id
								 })
							 }
						  },
						  fail(err) {
							reject(err);
						  }
						});
				}  
				
					
				
			
			  },
			   fail(res) {
			    
			    },
			});
		  },
		  fail(err) {
			reject(err);
		  }
		});   
	},
	/* orderPay:function(data){
		console.log('立即付款回调'+data)  
		
		//console.log('速度'+JSON.stringify(data.data))
		// tt.navigateTo({
		// 	url: '/pages/pay/pay?orderInfo='+JSON.stringify(data.data)
		// })
		console.log('结算'+data.data.alipay_url)
		tt.pay({
		  orderInfo: data.data,
		  service: 1,
		  _debug:1,
		  getOrderStatus(res) {
			  console.log('商户是的'+res)
		      let { out_order_no } = res;
		      return new Promise(function (resolve, reject) {
				 tt.switchTab({ url: '/pages/personal/personal' })	
		      });
		    },
		  success(res) {
			var postData = {
					out_order_no:data.data.out_order_no
				}
				tt.request({
				  url: "https://dy.nbhysj.com/api/getOrderStatus",
				  data: postData,
				  method: 'POST',
				  success(res) {
					  console.log('查询到了吗')
					  console.log('商户状态'+JSON.stringify(res.data.data.order_status))
					 // 商户后端查询的微信支付状态，通知收银台支付结果
					 if(res.data.data.order_status==2){
						 tt.navigateTo({
						 	url: '/pages/verification/verification?order_no='+res.data.data.id
						 })
					 }
				  },
				  fail(err) {
					reject(err);
				  }
				});
				
			//	call.request('api/getOrderStatus',postData,this.postOrder, this.fail);
		
		  },
		   fail(res) {
		      // 调起收银台失败处理逻辑
		    },
		});
		
	}, */
	
});